<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "punishment".
 *
 * @property integer $id
 * @property string $stu_id
 * @property string $year
 * @property integer $semester
 * @property string $date
 * @property string $situation
 * @property string $rank
 * @property string $filenum
 * @property string $name
 */
class PunishmentModel extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'punishment';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['stu_id'], 'required'],
            [['semester'], 'integer'],
            [['stu_id', 'year', 'date', 'name'], 'string', 'max' => 20],
            [['situation'], 'string', 'max' => 500],
            [['rank', 'filenum'], 'string', 'max' => 50],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'stu_id' => '学号',
            'year' => '学年',
            'semester' => '学期',
            'date' => '日期',
            'situation' => '违纪情况',
            'rank' => '处分等级',
            'filenum' => '文件号',
            'name' => '姓名',
        ];
    }
    /**
     * 保存导入excel文件内容
     * @param $data  传入文件内容的数组
     * @return bool  保存成功返回true
     *
     */
    public static function SaveImport($data){

        try{


            foreach ($data as $value){
                $person=new PunishmentModel();
                $person->stu_id=$value['学号'].'';//学号要转化为字符型才能通过验证
                $person->name=$value['姓名'].'';
                $person->year=$value['学年'].'';
                $person-> semester=(int)$value['学期'];
                $person->date=$value['日期'].'';
                $person->situation=$value['违纪情况'].'';
                $person->filenum=$value['文件号'].'';
                $person->rank=$value['处分等级'].'';

                $person->save();
            }
            return true;

        }catch (\Exception $e){
            throw $e;
        }

    }
}
